package com.sinosoft.dsp.system.to.helper;

import java.math.BigDecimal;
import java.util.Date;

import com.sinosoft.dsp.system.core.i18n.BaseMessages;
import com.sinosoft.dsp.system.to.attribute.Attribute;
import com.sinosoft.dsp.system.to.attribute.BigNumberType;
import com.sinosoft.dsp.system.to.attribute.BlobType;
import com.sinosoft.dsp.system.to.attribute.BooleanType;
import com.sinosoft.dsp.system.to.attribute.DateType;
import com.sinosoft.dsp.system.to.attribute.IntegerType;
import com.sinosoft.dsp.system.to.attribute.NumberType;
import com.sinosoft.dsp.system.to.attribute.StringType;

/**
 * 域属性的枚举类
 * 
 * @author 潘巍（Peter Pan）
 * @since 2011-2-4 下午08:25:28
 */
public enum CommonAttributeType {
	// 空类型
	TYPE_NONE("None", "Non", BaseMessages.getString("Attribute.TypeCode.None"),
			null, null),
	// 长整型
	TYPE_INTEGER("Integer", "Int", BaseMessages
			.getString("Attribute.TypeCode.Integer"), IntegerType.class,
			new Class<?>[] { Integer.class, Long.class }),
	// 数字类型
	TYPE_NUMBER("Number", "Num", BaseMessages
			.getString("Attribute.TypeCode.Number"), NumberType.class,
			new Class<?>[] { Double.class }),
	// 大数字类型
	TYPE_BIGNUMBER("BigNumber", "Bnum", BaseMessages
			.getString("Attribute.TypeCode.BigNumber"), BigNumberType.class,
			new Class<?>[] { BigDecimal.class }),
	// 布尔型
	TYPE_BOOLEAN("Boolean", "Bool", BaseMessages
			.getString("Attribute.TypeCode.Boolean"), BooleanType.class,
			new Class<?>[] { Boolean.class }),
	// 字符串型
	TYPE_STRING("String", "Str", BaseMessages
			.getString("Attribute.TypeCode.String"), StringType.class,
			new Class<?>[] { String.class }),
	// 字符类型
	TYPE_CHAR("Char", "Char",
			BaseMessages.getString("Attribute.TypeCode.Char"), null,
			new Class<?>[] { Character.class }),
	// 日期类型
	TYPE_DATE("Date", "Date",
			BaseMessages.getString("Attribute.TypeCode.Date"), DateType.class,
			new Class<?>[] { Date.class }),
	// 时间类型
	TYPE_TIME("Time", "Time",
			BaseMessages.getString("Attribute.TypeCode.Time"), DateType.class,
			null),
	// 日期时间类型
	TYPE_DATETIME("DateTime", "DT", BaseMessages
			.getString("Attribute.TypeCode.DateTime"), DateType.class, null),
	// 时间范围类型
	TYPE_DURATION("Duration", "Dura", BaseMessages
			.getString("Attribute.TypeCode.Duration"), null, null),
	// 字节流类型
	TYPE_BLOB("Blob", "Blob",
			BaseMessages.getString("Attribute.TypeCode.Blob"), BlobType.class,
			new Class<?>[] { byte[].class }),
	// 文本字节流类型
	TYPE_CLOB("Clob", "Clob",
			BaseMessages.getString("Attribute.TypeCode.Clob"), BlobType.class,
			new Class<?>[] { char[].class }),
	// 
	TYPE_TOKEN("Token", "Tok", BaseMessages
			.getString("Attribute.TypeCode.Token"), null, null),
	// 范围类型
	TYPE_RANGE("Range", "Ran", BaseMessages
			.getString("Attribute.TypeCode.Range"), null, null),
	// 数量类型
	TYPE_QUANTITY("Quantity", "Quan", BaseMessages
			.getString("Attribute.TypeCode.Quantity"), null, null),
	// 单位类型
	TYPE_UNIT("Unit", "Unit",
			BaseMessages.getString("Attribute.TypeCode.Unit"), null, null),
	// 行政区划类型
	TYPE_ADMINDIVISION("AdminDivision", "AD", BaseMessages
			.getString("Attribute.TypeCode.AdminDivision"), null, null),
	// 详细地址类型
	TYPE_DETAILEDADDRESS("DetailedAddress", "DA", BaseMessages
			.getString("Attribute.TypeCode.DetailedAddress"), null, null);

	private String code;

	private String shortCode;

	private String description;

	private Class<? extends Attribute<?>> toAttributeClass;

	private Class<?>[] acceptClasses;

	private CommonAttributeType(String code, String shortCode,
			String description, Class<? extends Attribute<?>> toAttributeClass,
			Class<?>[] acceptClasses) {
		this.code = code;
		this.shortCode = shortCode;
		this.description = description;
		this.toAttributeClass = toAttributeClass;
		this.acceptClasses = acceptClasses;
	}

	public String getCode() {
		return code;
	}

	public String getShortCode() {
		return shortCode;
	}

	public String getDescription() {
		return description;
	}

	public Class<? extends Attribute<?>> getToAttributeClass() {
		return toAttributeClass;
	}

	public Class<?>[] getAcceptClasses() {
		return acceptClasses;
	}

	/**
	 * 通过代码获得类型
	 * 
	 * @param code
	 *            代码
	 * @return
	 */
	public static final CommonAttributeType getTypeByCode(String code) {
		for (CommonAttributeType cat : CommonAttributeType.values()) {
			if (cat.code.equals(code)) {
				return cat;
			}
		}
		return CommonAttributeType.TYPE_NONE;
	}

	/**
	 * 通过值对象，判断适用那种类型
	 * 
	 * @param obj
	 * @return
	 */
	public static final CommonAttributeType getTypeByObject(Object obj) {
		for (CommonAttributeType cat : CommonAttributeType.values()) {
			Class<?>[] cz = cat.getAcceptClasses();
			if (cz != null) {
				for (Class<?> c : cz) {
					if (obj.getClass() == c) {
						return cat;
					}
				}
			}
		}
		return CommonAttributeType.TYPE_NONE;
	}
}
